package basic

import (
	"fmt"
	"reflect"
)

type person struct {
	name string
}

func Reflect() {
	//var a float32 = 3.64
	//fmt.Print(reflect.TypeOf(a))
	//p := person{
	//	name: "cpp",
	//}
	//fmt.Print(reflect.TypeOf(p), reflect.TypeOf(p).Name(), reflect.TypeOf(p).Kind())
	//res := reflect.ValueOf(a).Kind()
	//if res == reflect.Float32 {
	//	fmt.Print(111)
	//}
	//fmt.Print(res, reflect.ValueOf(a))
	//
	//b := 20
	//reflectSetValue1(&b)
	//fmt.Print(b)
	//var c *int
	//fmt.Println("var a *int IsNil:", reflect.ValueOf(c).IsNil())
	//fmt.Print(c)
	//fmt.Println("nil IsValid:", reflect.ValueOf(nil).IsValid())
	// map
	e := map[string]int{}
	e["test"] = 11
	fmt.Print(reflect.ValueOf(e).MapIndex(reflect.ValueOf("test")).IsValid())
	f := struct{}{}
	fmt.Print(reflect.ValueOf(f).FieldByName("name").IsValid())
	fmt.Print(reflect.ValueOf(f).MethodByName("getName").IsValid())
	stu := stu{
		Name:  "小王子",
		Score: 90,
	}
	g := reflect.TypeOf(stu)
	k := reflect.ValueOf(stu)
	fmt.Print(g.Name(), g.Kind())
	for i := 0; i < g.NumField(); i++ {
		field := g.Field(i)
		fmt.Print(field)
	}

	for i := 0; i < g.NumMethod(); i++ {
		method := g.Method(i)
		fmt.Print(method.Name)
		var args []reflect.Value
		k.Method(i).Call(args)
	}

}

type stu struct {
	Name  string `json:"name"`
	Score int    `json:"score"`
}

func (s stu) Study() string {
	msg := "好好学习，天天向上。"
	fmt.Println(msg)
	return msg
}

func (s stu) Sleep() string {
	msg := "好好睡觉，快快长大。"
	fmt.Println(msg)
	return msg
}

func reflectSetValue1(x interface{}) {
	v := reflect.ValueOf(x)
	v.Elem().SetInt(100)
	fmt.Print(v.Elem())
}
